Imports DAL
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Object
Imports System.Configuration
Imports Microsoft.VisualBasic.Strings
Imports MyClasses

Public Class frmDatabaseBackup

    Sub SetProgress(ByVal iVal As Integer)
        ProgressBar1.Value = iVal
        Me.Refresh()
    End Sub

    Sub ShowProgressBar()
        ProgressBar1.Visible = True
        lblPleasewait.Visible = True
    End Sub

    Sub HideProgressBar()
        ProgressBar1.Visible = False
        lblPleasewait.Visible = False
    End Sub

    Private Sub frmDatabaseBackup_HelpButtonClicked(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.HelpButtonClicked
        'Rauhl_ITS@20073012 : Add following code display help
        Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Database Backup")
        e.Cancel = True
    End Sub

    Private Sub frmDatabaseBackup_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        'Rauhl_ITS@20073012 : Add following code display help
        If e.KeyCode = Keys.F1 Then
            e.SuppressKeyPress = True
            Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Database Backup")
        End If
    End Sub

    Private Sub frmBackup_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Call BCommon.SetFormStandards(Me, False)
        HideProgressBar()
        chkPath.Checked = False
        chkPath.Visible = False
        txtBackupDir.Text = My.Application.Info.DirectoryPath.Substring(0, 2) + "\Temp\" + Format(DateTime.Now, "ddMMyyhhmmss") + "\"
    End Sub


    Private Sub cmdExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackUp.Click
        'cmdConnect_Click(sender, e)
        Try
            Me.Cursor = Cursors.WaitCursor
            If chkData.Checked = False And chkStructure.Checked = False Then
                'MessageBox.Show("Please select structure or data to backup.", "Check", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                MessageBox.Show(BCommon.GetMessage(10387), ConfigurationManager.AppSettings("MsgTitle"), MessageBoxButtons.OK, MessageBoxIcon.Stop)
                Exit Sub
            End If

            If ChkFull.Checked = False And chkMaster.Checked = False And chkSystem.Checked = False Then
                BCommon.ShowMsgError(10387)
                'MessageBox.Show(BCommon.GetMessage(10387), ConfigurationManager.AppSettings("MsgTitle"), MessageBoxButtons.OK, MessageBoxIcon.Stop)
                Exit Sub
            End If

            ShowProgressBar()
            ProgressBar1.Maximum = 8
            SetProgress(1)
            Me.Refresh()
            If chkPath.Checked = True Then
                'Default path for backup
                txtFileName.Enabled = False
                txtFileName.Text = "D:\" & Format(DateTime.Now, "ddMMyyhhmmss") & ".SQLBackup"
                'Export to selected file
                Export_Database(txtFileName.Text)
            Else
                'Prompt user for name of backup file.
                txtFileName.Enabled = True
                Dim sFileName As String = BCommon.AskSaveAsFile()
                If sFileName <> "" Then
                    btnBackUp.Enabled = False
                    'Set backup file path.
                    txtFileName.Text = sFileName
                    'Export to selected file
                    Export_Database(txtFileName.Text)
                End If
            End If
            HideProgressBar()
            btnBackUp.Enabled = True
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Sub Export_Database(ByVal FileName As String)
        Try
            ''CHECK IF BACKUP DIRECTORY EXISTS OR NOT. IF IT EXISTS, THEN DELETE IT.
            ''NOTE THAT THIS BACKUP DIRECTORY IS A TEMPORARY DIRECTORY WHICH WAS INITIALIZED IN DURING FORM LOAD.
            If My.Computer.FileSystem.DirectoryExists(txtBackupDir.Text) Then
                My.Computer.FileSystem.DeleteDirectory(txtBackupDir.Text, FileIO.DeleteDirectoryOption.DeleteAllContents)
            End If
            ''CREATE TEMPORARY DIRECTORY.
            If Not My.Computer.FileSystem.DirectoryExists(txtBackupDir.Text) Then
                My.Computer.FileSystem.CreateDirectory(txtBackupDir.Text)
            End If
            'INITIALIZE A DATATABLE. THIS DATATABLE WILL HOLD ALL DATABASE OBJECTS AND THEIR SCRIPTS.

            SetProgress(4)
            'FOLLOWING PROCEDURE WILL GENERATE SCRIPTS FOR SELECTED TABLES USING SQL-DMO. THIS PROCEDURE ALSO BACK UP DATA USING BCP FUNCTIONS IN SQL-DMO LIBRARY.
            'SCRIPT FILE AND DATA FILES WILL BE SAVED IN TEMPORARY FOLDER.

            'BBackup.GenerateFiles(DTScripts, chkData.Checked, "table", chkStructure.Checked, chkMaster.Checked, chkTrans.Checked, FileName)
            Dim Result As Boolean
            Result = BBackup.GenerateFiles(txtBackupDir.Text, chkData.Checked, "table", True, chkMaster.Checked, chkTrans.Checked, ChkFull.Checked, chkSystem.Checked, FileName)
            SetProgress(6)
            My.Computer.FileSystem.DeleteDirectory(txtBackupDir.Text, FileIO.DeleteDirectoryOption.DeleteAllContents)
            SetProgress(8)
            If Result Then
                MessageBox.Show(BCommon.GetMessage(10388), ConfigurationManager.AppSettings("MsgTitle"), MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show(BCommon.GetMessage(10389), ConfigurationManager.AppSettings("MsgTitle"), MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
            HideProgressBar()
            'Since script for all objects and data are now saved to temporary directory, zip this temporary folder to a file with extension *.SQLBACKUP.
        Catch ex As Exception

        End Try

    End Sub

   
    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        Me.Close()
    End Sub

    Private Sub chkPath_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkPath.CheckedChanged
        Try
            If chkPath.Checked = True Then
                txtFileName.Enabled = True
                txtFileName.ReadOnly = True
                txtFileName.Enabled = False
                txtFileName.Clear()
            Else
                txtFileName.Enabled = False
                txtFileName.ReadOnly = False
                txtFileName.Enabled = True
            End If
        Catch ex As Exception

        End Try
    End Sub

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub chkMaster_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkMaster.CheckedChanged
        Try
            
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "chkMaster_CheckedChanged")
        End Try
    End Sub

    Private Sub chkSystem_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSystem.CheckedChanged
        Try

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "chkSystem_CheckedChanged")
        End Try

    End Sub
End Class

'Dim OledbConnectionString As String
' Dim oScript As New clsScript
'Dim oScript As New DBackup
'Dim db As SQLDMO.Database2

'Public Shared Function AskDirectory() As String
'    Dim folderBrowse As New FolderBrowserDialog
'    If folderBrowse.ShowDialog() = DialogResult.OK Then
'        Return folderBrowse.SelectedPath
'    Else
'        Return ""
'    End If
'End Function

'Public Shared Function AskSaveAsFile() As String
'    Dim fileD As New SaveFileDialog
'    'fileD.Filter = "SQLBackup Files | *." + clsCommon.FileExtension 'Temp commented
'    If fileD.ShowDialog() = DialogResult.OK Then
'        Return fileD.FileName
'    Else
'        Return ""
'    End If
'End Function

'Public Shared Function AskOpenFile() As String
'    Dim fileD As New OpenFileDialog
'    'fileD.Filter = "SQLBackup Files | *." + clsCommon.FileExtension    'Temp commented
'    If fileD.ShowDialog() = DialogResult.OK Then
'        Return fileD.FileName
'    Else
'        Return ""
'    End If
'End Function